package com.bawei.day11_contentprovider_b;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt_select;
    private Button bt_insert;
    private Button bt_update;
    private Button bt_delete;
    private Uri  uri = Uri.parse("content://com.bawei.day11_contontprovider_a");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        bt_select = (Button) findViewById(R.id.bt_select);
        bt_insert = (Button) findViewById(R.id.bt_insert);
        bt_update = (Button) findViewById(R.id.bt_update);
        bt_delete = (Button) findViewById(R.id.bt_delete);

        bt_select.setOnClickListener(this);
        bt_insert.setOnClickListener(this);
        bt_update.setOnClickListener(this);
        bt_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_select:
                select();

                break;
            case R.id.bt_insert:
                insert();

                break;
            case R.id.bt_update:
                update();

                break;
            case R.id.bt_delete:
                delete();

                break;
        }
    }

    private void delete() {
        ContentResolver contentResolver = getContentResolver();
        int delete = contentResolver.delete(uri, "id = ?", new String[]{2 + ""});
        Toast.makeText(this, ""+delete, Toast.LENGTH_SHORT).show();
    }

    private void update() {
        ContentResolver contentResolver = getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","杨东峰");
        int update = contentResolver.update(uri,contentValues,"id = ?",new String[]{2+""});
    }

    private void select() {
        ContentResolver contentResolver = getContentResolver();
         Cursor cursor = contentResolver.query(uri, null, null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                int age = cursor.getInt(cursor.getColumnIndex("age"));
                Log.d("ytx", " id: "+id+"    name:"+name+"   age:"+age);
            }
        }
    }
    private void insert() {
        ContentResolver contentResolver = getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","韦德里");
        contentValues.put("age",18);
        contentResolver.insert(uri,contentValues);
    }
}
